package com.apes.hr.recruit.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.*;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.repository.EnumerationRepository;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：应聘申请单
 * @author HuJianlin
 * @create 2018-05-05 15:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ApplyJobOrder.CONST_TABLE_NAME)
public class ApplyJobOrder extends Model {
    public static final String CONST_TABLE_NAME = "hr_Apply_Job_Order";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "入职申请单", prefix = "A", padding = 9)
    @Comment("申请单号")
    private String id;   //申请单号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id", referencedColumnName = "id")
    @Comment("员工信息")
    private Employee employee; //员工信息

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "talent_pool_id", referencedColumnName = "id")
    @Comment("人才信息")
    private TalentPool talentPool; //人才信息

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recruit_order_id", referencedColumnName = "id")
    @Comment("招聘单号")
    private RecruitOrder recruitOrderId;  //招聘单号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recruit_channel_id", referencedColumnName = "id")
    @Comment("招聘渠道")
    private RecruitChannel recruitChannel;  //招聘渠道

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cognitive_Employee_id", referencedColumnName = "id")
    @Comment("认识的员工")
    private Employee cognitiveEmployee;   //认识的员工

    @Column(name = "cognitive_relation", length = 16)
    @Comment("与该员工的关系")
    private String cognitiveRelation;  //与该员工的关系

    @Column(name = "introducer", length = 16)
    @Comment("推荐人")
    private String introducer;   //推荐人

    @Column(name = "introducer_relation", length = 16)
    @Comment("与推荐人的关系")
    private String introducerRelation;  //与推荐人的关系

    @Column(name = "adept_technology", length = 64)
    @Comment("擅长技术")
    private String adeptTechnology;  //擅长技术

//    @Column(name = "entry_place", length = 32)
//    @Comment("拟入职地点")
//    private String entryPlace;    //拟入职地点

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "on_duty_state", referencedColumnName = "id")
    @Comment("在职状态")
    private Enumeration onDutyState; //在职状态(onDutyState)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "choose_social_security", referencedColumnName = "id")
    @Comment("购买社保意向")
    private Enumeration chooseSocialSecurity;    //购买社保意向

    @Column(name = "same_trade_keep_out", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("竞争行业禁止协议")
    private boolean sameTradeKeepOut = false; //是否有签《竞争行业禁止协议》

    @Column(name = "has_other_job", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否与其他单位存在劳动关系")
    private boolean hasOtherJob = false;  //是否与其他单位存在劳动关系

//    @Column(name = "apply_state", nullable = false, columnDefinition = "number(8,0) default 0")
//    private int applyState = 0; //应聘状态(0:提交申请；1：预约初试；2：初试通过；3：预约复试；4：复试通过；5：预约入职；6：已入职；7：面试不通过；8：流失)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "test_examiner_id", referencedColumnName = "id")
    @Comment("初试官姓名")
    private PartyRole testExaminer;  //初试官姓名----改为人资面试官

    @Column(name = "test_judge", length = 4000)
    @Comment("初试评价")
    private String testJudge;  //初试评价

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "retest_examiner_id", referencedColumnName = "id")
    @Comment("用人部门面试官")
    private PartyRole retestExaminer;  //第三面试官姓名 ---改为用人部门面试官

    @Column(name = "retest_judge", length = 4000)
    @Comment("用人部门评价")
    private String retestJudge;  //第三面试官评价---改为用人部门评价

    @Column(name = "return_date")
    @Comment("退回日期")
    private Date returnDate; //退回日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "return_uid", referencedColumnName = "id")
    @Comment("退回人")
    private PartyRole returnUid; //退回人

    @Column(name = "approve_date")
    @Comment("确认报道日期")
    private Date approveDate; // 确认报道日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("确认人")
    private PartyRole approveUid; // 确认人

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate; //删除日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUid; //删除人

    @Column(name = "cancel_date")
    @Comment("取消报道日期")
    private Date cancelDate; //取消报道日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cancel_uid", referencedColumnName = "id")
    @Comment("取消报道人")
    private PartyRole cancelUid; //取消报道人

    @Column(name = "state", length = 10, nullable = false)
    @Comment("单据状态")
    private String state; //单据状态： ('create', '制单'), ('done', '确认进入'), ('delete', '删除')   //* ('retreat', '退回')  *('return', '退回')
    //('firstTrial', '审核'),('cancel', '放弃报到'), ('depart', '离职')

    @Column(name = "probation_pay", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("试用期工资")
    private double probationPay;    // 试用期工资

    @Column(name = "regular_pay", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("转正后工资")
    private double regularPay;    // 转正后工资

    @Column(name = "proba_min_pay", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("试用期保底工资")
    private double probaMinPay;    // 试用期保底工资

    @Column(name = "regular_min_pay", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("转正后保底工资")
    private double regularMinPay;    // 转正后保底工资

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "work_post_id", referencedColumnName = "id")
    @Comment("岗位")
    private WorkPost workPost; //岗位

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "job_id", referencedColumnName = "id")
    @Comment("职位")
    private Job job; //职位

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "rank_id", referencedColumnName = "id")
    @Comment("职级")
    private Rank rank; //职级

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "agree_entry_date")
    @Comment("约定入职时间")
    private Date agreeEntryDate;    // 约定入职时间

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "first_payday")
    @Comment("首个计薪日")
    private Date firstPayday; // 首个计薪日

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "Probation_periods")
    @Comment("实习二期")
    private Date probationPeriods;    // 试用期 (实习二期)

//    @Column(name = "Probation_period", nullable = false)
//    private Date probationPeriod;// 试用期

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "probation_begin")
    @Comment("试用期起")
    private Date probationBegin; // 试用期起

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "probation_end")
    @Comment("试用期止")
    private Date probationEnd;  // 试用期止

//    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
//    @Temporal(TemporalType.DATE)
//    @Column(name = "contract_begin")
//    @Comment("合同期起")
//    private Date contractBegin;    // 合同期起

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "labour_begin")
    @Comment("实习期/劳务期起")
    private Date labourBegin;    //实习期/劳务期起

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "labour_end")
    @Comment("实习期/劳务期止")
    private Date labourEnd;    // 实习期/劳务期止

    @Column(name = "is_2nd_entry", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否二次入职")
    private boolean twoNdEntry = false;    // 是否二次入职

    @Column(name = "school")
    @Comment("毕业院校")
    private String school; //毕业院校

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    @Comment("入职部门")
    private Branch branch; //入职部门


    @Column(name = "first_trial_Date")
    @Comment("审核日期")
    private Date firstTrialDate; //审核日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "first_trial_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole firstTrialUid; // 审核人

    @Column(name = "abandon", length = 4000)
    @Comment("放弃报到原因")
    private String abandon;  //放弃报到原因


    @Column(name = "unacceptable_id", length = 4000)
    @Comment("不能接受的企业行为")
    private String unacceptables;  //不能接受的企业行为


    public void setUnacceptables(String unacceptables) {
    }

    @Transient
    private Set<Enumeration> unacceptableSet;

    public Set<Enumeration> getunacceptableSet() {
        if (isWrite() || isLoad()) {
            unacceptableSet = getUnacceptable(this.unacceptables);
        }
        return unacceptableSet;
    }

    public void setunacceptableSet(Set<Enumeration> unacceptableSet) {
        this.unacceptableSet = unacceptableSet;
        if (!isWrite()) {
            this.unacceptables = toConvertUnacceptable(this.unacceptableSet);
        }
    }

    @Transient
    private Set<Enumeration> getUnacceptable(String unacceptables) {
        if (StringUtil.isEmpty(unacceptables)) return null;
        Set<Enumeration> persons = new HashSet<>();
        String[] rows = unacceptables.split(";");
        for (String person : rows) {
            if (person.contains("|")) {
                persons.add(SpringManager.getBean(EnumerationRepository.class).findOne(StringUtils.split(person, "|")[0]));
            }
        }
        return persons;
    }

    private String toConvertUnacceptable(Set<Enumeration> unacceptableSet) {
        StringBuffer sb = new StringBuffer();
        for (Enumeration enumeration : unacceptableSet) {
            if (sb.length() > 0) {
                sb.append(";");
            }
            sb.append(enumeration.getId());
            sb.append("|");
            sb.append(enumeration.getDescription());  // 格式 id|description;
        }
        return sb.toString();
    }

    @Column(name = "invoking", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否被聘用合同调用")
    public boolean invoking = false;//是否被聘用合同调用

    @Column(name = "annual_salary",columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("年薪")
    public double annualSalary ;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "degree_id", referencedColumnName = "id")
    @Comment("学历")
    public Enumeration degree; //学历

   @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "hr_examiner_id", referencedColumnName = "id")
    @Comment("第三面试官")
    private PartyRole hrExaminer;  //人资面试官姓名 ---改为第三面试官

    @Column(name = "hr_judge", length = 4000)
    @Comment("第三面试官评价")
    private String hrJudge;  //人资评价 ----改为第三面试官

    //是否需要钉钉标记
    @Transient
    private Boolean isNeedDingDing = false;


}
